<?php

class Media extends Admin_Controller {

	function __construct()
	{
		parent::__construct();	
	}
	
	
	public function index()
	{
		$this->aPageJs[] = array(
							'combine_js' => true,
							'base_href' => '/media/js',
							'js' => array('admin_media'),
							'version' => '2.0'
		);
		
		$this->display_assets();
		
		$this->display();
		
		$this->load->view('platform/'.$this->sPlatform.'/admin/media/context_menu', $this->aData);
	}
	
	
	public function display_assets()
	{
		$this->aPageJs[] = array(
							'combine_js' => true,
							'base_href' => '/media/js',
							'js' => array('jquery_jeditable', 'jquery_treeview', 'swfobject', 'jquery_uploadify_min', 'jquery_contextmenu'),
							'version' => '2.0'
		);
		
		$this->aPageCss[] = array(
							'combine_css' => true,
							'base_href' => '/media/css',
							'css' => array('jquery_treeview', 'jquery_uploadify', 'admin_media'),
							'media' => 'all',
							'version' => '1.0'
		);
	}
	
	
	public function display()
	{
		$this->load->library('media_lib');
		$this->load->model('db/media_folders', 'media_folders');
		
		$qFolders = $this->media_lib->get_folder_hierarchy(array('folder_parent_id' => NULL), array());
		
		$sMediaFolderTree = $this->media_lib->build_folder_hierarchy($qFolders, 'ul', 'folder_container', 'li', 'folder', true, '#');
			
		$this->aData['sMediaFolderTree'] = $sMediaFolderTree;
		
		$this->load->view('platform/'.$this->sPlatform.'/admin/media/index', $this->aData);
	}
	
	
	public function popup()
	{
		$this->sSkinFile = 'popup';
		
		$sJsCallback = $this->uri->segment(4);
		
		if ($sJsCallback !== false)
		{
			$this->aPageJs[] = "
			
			var callback_func = '".$sJsCallback."';
			
			";
		}

		$this->aPageJs[] = array(
							'combine_js' => true,
							'base_href' => '/media/js',
							'js' => array('admin_media_popup'),
							'version' => '2.0'
		);
		
		$this->display_assets();
		
		$this->display();
		
		$this->load->view('platform/'.$this->sPlatform.'/admin/media/contaxt_menu_popup', $this->aData);
	}


	public function edit_media()
	{

		$this->load->library('image_lib');
		$this->aPageJs[] = array(
							'combine_js' => true,
							'base_href' => '/media/js',
							'js' => array('jquery_ui_core', 'jquery_ui_slider', 'jquery_jcrop', 'jquery_hotkeys', 'admin_edit_media'),
							'version' => '1.0'
		);	

		// build up our header_css array
		$this->aPageCss[] = array(
							'combine_css' => true,
							'base_href' => '/media/css',
							'css' => array('jquery_ui', 'jquery_jcrop', 'admin_edit_media'),
							'media' => 'all',
							'version' => '1.0'
		);		

		
		$this->sSkinFile = 'popup';
		
		$aUri = $this->uri->uri_to_assoc(2);
		
		$iMediaId = 0;
		
		if (array_key_exists('media_id', $aUri) === true)
		{
			$iMediaId = $aUri['media_id'];	
		}
		
		$this->load->model('db/views/media_by_folders', 'vw_media_by_folders');
		
		$this->vw_media_by_folders->media_id = $iMediaId;
		$this->vw_media_by_folders->dimension_type = 'W';
		
		$qMedia = $this->vw_media_by_folders->get(array(), 1);
		
		if ($qMedia->num_rows() === 0)
		{
			// TODO: throw an error - media not found
		}
		else 
		{
			
			$this->aPageJs[] = '
				var oMedia = { 
					iMediaId : '.$iMediaId.'
				};
			';
			
			$oMedia = $qMedia->row(0);
			
			$this->aData['oMedia'] = $oMedia;
			
			$this->load->view('platform/'.$this->sPlatform.'/admin/media/edit_media', $this->aData);
		}

	}
	
}

?>